import Ux from "ux";
import {Icon, Tooltip} from "antd";

import Do from "./wf.action.data";
import Ch from "../channel";
import React from "react";
import ExLinkage from "../../ecosystem/ExLinkage/UI";
import ExBpmn from "../../ecosystem/ExBpmn/UI";
import TxPhase from "../../ecosystem/TxPhase/UI";
import TxHistory from "../../ecosystem/TxHistory/UI";

const jsxListFn = (reference) => ({
    // 函数模式
    serial: (inherit = {}) => {
        const {data = {}, config = {}} = inherit;
        const {prefix = ""} = config;
        const title = Ux.formatExpr(prefix, data, true);
        return (
            <span className={"ex-workflow-serial"}>
                {data.serial}
                &nbsp;
                <Tooltip title={title} overlayClassName={"ex-workflow-tooltip"}>
                    <Icon type={"link"}/>
                </Tooltip>
            </span>
        )
    },
    phase: (inherit = {}) => {
        const $workflow = Ux.ambValue(reference, "$workflow");
        const config = Do.configUi($workflow, "phase");
        const {data = {}} = inherit;
        return (
            <TxPhase config={config} value={data.phase}/>
        )
    }
})

const jsxLinkageFn = (reference, field) => (ref) => {
    const $workflow = Ux.ambValue(reference, "$workflow");
    const {$inited = {}, $mode} = ref.props;
    const inherit = Ch.yoAmbient(reference);
    inherit.$inited = $inited;

    const {config = {}} = $workflow;
    const configLinkage = config.linkage ? config.linkage : {};
    inherit.config = configLinkage[field] ? configLinkage[field] : {};
    inherit.$mode = $mode
    inherit.$renders = jsxListFn(reference);
    // koSelection 构造
    const $config = inherit.config ? inherit.config : {};
    const {selection = {}} = $config.editor ? $config.editor : {};
    const $plugins = {};
    if (Ux.isNotEmpty(selection)) {
        $plugins.koSelection = (record = {}) => {
            let allow = true;
            const keys = Object.keys(selection);
            for (let idx = 0; idx < keys.length; idx++) {
                const field = keys[idx];
                const expected = selection[field];
                // 默认值包含
                if (expected.includes(null)) {
                    expected.push(undefined);
                }
                const checked = expected.includes(record[field]);
                if (!checked) {
                    allow = false;
                    break;
                }
            }
            return allow;
        }
    }
    inherit.$plugins = $plugins;
    // linkage 补充
    const $initial = {};
    $initial.region = $workflow['definitionKey'];
    inherit.$initial = $initial;
    return (
        <ExLinkage {...inherit}/>
    )
}
const jsxPhaseFn = (reference) => (ref, jsx) => {
    // 根据 $workflow 执行计算
    // 拷贝
    jsx = Ux.clone(jsx);
    if (!jsx.config) jsx.config = {};
    const $workflow = Ux.ambValue(reference, "$workflow");
    /*
     * optionJsx.config + phase 合并成最终的 config 属性
     * 该字段比较特殊，不包含其他属性信息
     * 1）工单信息在流程中属于共享属性，所以操作过程中不可以触发 onChange
     * 2）而在列表和表单渲染过程中，该状态会根据流程定义而有所改变
     */
    const config = Do.configUi($workflow, "phase");
    Object.assign(jsx.config, config);
    return (
        <TxPhase {...jsx} reference={ref}/>
    )
}
const jsxBpmnFn = (reference) => () => {
    /*
     * 只能从状态中拿 $workflow 专用变量
     * 旧代码：Ux.ambValue(reference, "$workflow");
     * 此处不执行二选一的完结操作
     */
    const {$workflow = {}} = reference.state;
    const {$inited = {}} = reference.props;
    const $bpmn = {
        data: $workflow['bpmn'],
        task: $workflow.task,
        trace: $workflow.history,
        phase: $inited.phase
    };
    const canvas = Do.configUi($workflow, "canvas");
    return (
        <ExBpmn {...$bpmn} $canvas={canvas}/>
    )
}
const jsxHistoryFn = (reference) => () => {
    const {$inited = {}} = reference.props;
    const data = $inited.history ? $inited.history : [];
    data.forEach(history => {
        // 开单人
        history.openBy = $inited.openBy;
    });
    return (
        <TxHistory data={data}/>
    )
}
export default {
    jsxLinkageFn,
    jsxListFn,
    // 表单中专用方法（字段级）
    jsxPhaseFn,
    jsxBpmnFn,
    jsxHistoryFn,
}